package com.alipay.app.groupmeal.standard.ui.c;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.base.BaseMVCActivity;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.ui.c.pre.ReadCardPre;
import com.alipay.app.groupmeal.standard.ui.b.util.WIth;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;

/**
 * 刷卡后查询FK表信息显示页面
 */
public class ReadCardActivity extends BaseMVCActivity {
    private ReadCardPre readCardPre;

    private ReadCardPre readCardPre2;

    private String cardSn;

    Disposable mdDisposable;

    public static void startAct(Context context, String cardsn) {
        Intent intent = new Intent(context, ReadCardActivity.class);
        intent.putExtra("cardsn", cardsn);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_checkfk_ft2;
    }

    @Override
    protected void initView() {
        cardSn = getIntent().getStringExtra("cardsn");
        mdDisposable = Flowable.intervalRange(0, 6, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        String btnText = (6 - aLong) + "秒";
                        readCardPre.showSec(btnText);
                        readCardPre2.showSec(btnText);

                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        finish();
                    }
                })
                .subscribe();
    }

    @Override
    protected void initData() {
        new WIth(mActivity).readCard(cardSn, new BlockingBaseObserver<Result<FkBean>>() {
            @Override
            public void onNext(@NonNull Result<FkBean> fkBeanResult) {
                readCardPre.showRes(fkBeanResult.getResult());
                readCardPre2.showRes(fkBeanResult.getResult());
                System.out.println("成功");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("失败");
                readCardPre.showError(e.getMessage());
                readCardPre2.showError(e.getMessage());
            }
        });
    }

    @Override
    protected void dissmissPresnetation() {
        if (readCardPre != null) {
            readCardPre.dismiss();
        }
        if (readCardPre2 != null) {
            readCardPre2.dismiss();
        }
    }
    @Override
    protected void onDestroy() {
        if (mdDisposable != null && !mdDisposable.isDisposed()) {
            mdDisposable.dispose();
            mdDisposable = null;
        }
        super.onDestroy();

    }
    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        finish();
        return super.onKeyEventBack(event);
    }

    @Override
    public void initPresentation() {
        super.initPresentation();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        if (mMainDisplay != null) {
            readCardPre = new ReadCardPre(mActivity, mMainDisplay,0);
            readCardPre.setOwnerActivity(this);
            showPresentationAtMain(readCardPre);

        }
        if (mDisplay != null) {
            readCardPre2 = new ReadCardPre(mActivity, mDisplay,1);
            registerPresentation(readCardPre2);
            showPresentation(readCardPre2);
        }
    }
}
